<template lang="pug">
//- 
  示例：
  aprivacy(:ref="el=>data.Privacy=el")

  watch(()=>data.Privacy, P=>{
    wx.requirePrivacyAuthorize()
  })
view.a-privacy.fixed.full.flex.center(@touchmove.stop.prevent v-if="data.show")
  view.bg.white.mod.box.radius
    view.bold.m-b-20 {{ title }}
    view.desc.article
      text.txt(v-for="n,i in desc" @click="methods._desc(i)" :class="{wrap: descWrap, blue: privacyInDescInex.includes(i)}") {{ n }}
    view.flex
      button.clear.btn.gray(id="disagree-btn" @click="methods._choose(false)") 不同意
      view.p-l-25
      button.clear.btn(id="agree-btn" open-type="agreePrivacyAuthorization" @agreeprivacyauthorization="methods._choose(true)") 同意
</template>
<script setup>
import {reactive} from 'vue'
import {onLoad} from '@dcloudio/uni-app'

const emits = defineEmits(['choose'])
const props = defineProps({
  title: {type: String, default: '用户隐私保护提示'},

  desc: {type: Array, default: [
    '感谢您使用本系统，您使用前应阅读井同意',
    '《隐私协议》',
    '当您点击同意并开始时用服务时，即表示你已理解并同息该条款内容，该条款将对您产生法律约束力。如您拒绝，将无法使用本系统。'
  ]}, // 隐私弹窗内容
  descWrap: {type: Boolean, default: false}, // 隐私弹窗描述内容是否按数组是否换行
  privacyInDescInex: {type: Array, default: [1]}, // 隐私政策标题在desc中的索引
})

const data = reactive({
  show: false,
})
const pureData = {
  privacyResolves: new Set(),
}
const methods = {
  _desc(i){
    if(props.privacyInDescInex.includes(i)){
      wx.openPrivacyContract()
    }
  },
  _choose(result){
    data.show = false
    pureData.privacyResolves.forEach(resolve => {
      resolve(result? {event: 'agree', buttonId: 'agree-btn'}: {event: 'disagree'})
    })

    emits('choose', result)
  }
}
onLoad(()=>{
  //https://developers.weixin.qq.com/miniprogram/dev/api/open-api/privacy/wx.getPrivacySetting.html
  wx.getPrivacySetting({
    success(res){
      console.log( res.needAuthorization )
      if(res.needAuthorization){
        data.show = true
      }else{
        methods._choose(true)
      }
    },
    fail(err){
      console.log(err)
    }
  })
  //https://developers.weixin.qq.com/miniprogram/dev/api/open-api/privacy/wx.onNeedPrivacyAuthorization.html
  wx.onNeedPrivacyAuthorization(resolve=>{
    console.log('隐私接口')
    pureData.add(resolve)
  })
})

defineExpose({
  show(show){
    data.show = show
  }
})
</script>
<style lang="scss" scoped>
@use '/common/scss/mixin.scss' as *;
.a-privacy{
  --mask: rgba(0,0,0,0.15);
  --btn-margin-top: 60rpx;
  --mod-padding-left: 40rpx;
  --mod-padding-top: 50rpx;
  .box{width: 600rpx;}
  .desc{
    .wrap{
      display: block;
    }
  }
}
</style>